<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i> 基础表格</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
               <!--  <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button> -->
                <el-select v-model="select_cate" placeholder="筛选状态" class="handle-select mr10" @change="select_State">
                    <el-option key="1" label="执行中" value="ACQUIRED"></el-option>
                    <el-option key="2" label="暂停" value="PAUSED"></el-option>
                    <el-option key="3" label="等待中" value="WAITING"></el-option>
                    <el-option key="4" label="出错" value="ERROR"></el-option>
                </el-select>
                <el-input v-model="select_word" placeholder="筛选任务名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="primary" icon="search" @click="handleEdit">添加任务</el-button>
            </div>
            <el-table :data="data" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="55"></el-table-column> -->
                <el-table-column prop="jobName" label="job名称" sortable width="150">
                </el-table-column>
                <el-table-column prop="jobGroup" label="job所在组名" width="120">
                </el-table-column>
                <el-table-column prop="description" label="job描述" width="120">
                </el-table-column>
                <el-table-column prop="jobClassName" label="执行类" width="200">
                </el-table-column>
                <el-table-column prop="cronExpression" label="cron表达式" width="120">
                </el-table-column>
                 <el-table-column prop="nextTime" label="下次触发时间" width="170">
                </el-table-column> -->
                <el-table-column prop="prevTime" label="上次触发时间" width="170">
                </el-table-column>
                <el-table-column prop="prevTime" label="操作" width="310" >
                    <template slot-scope="scope">
                        <el-button size="small" @click="trigger(scope.$index, scope.row)">触发</el-button>
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        <el-button v-text="p_r_Formatter(scope.row)" size="small" @click="p_r_switch(scope.$index, scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="dialog_title" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                 <el-form-item label="job名称">
                    <el-input v-model="form.jobName"></el-input>
                </el-form-item>
                <el-form-item label="job组名">
                    <el-input v-model="form.jobGroup"></el-input>
                </el-form-item>
                <el-form-item label="job描述">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
                <el-form-item label="触发器名称">
                    <el-input v-model="form.triggerName"></el-input>
                </el-form-item>
                <el-form-item label="触发器组名">
                    <el-input v-model="form.triggerGroup"></el-input>
                </el-form-item>

                <el-form-item label="执行类">
                    <el-input v-model="form.jobClassName"></el-input>
                </el-form-item>
                <el-form-item label="cron表达式">
                    <el-input v-model="form.cronExpression"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>


<script>
    export default {
        name: 'basetable',
        data() {
            return {
                get_url: 'http://localhost:8081/getList',
                del_url: 'http://localhost:8081/delete',
                save_url: 'http://localhost:8081/saveOrUpdate',
                trigger_url: 'http://localhost:8081/trigger',
                pause_resume_url: 'http://localhost:8081/pauseOrResume',
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                dialog_title: '',
                isAdd: true, // 是否是新增
                total:1, // 分页页数
                form: {
                    jobName: '',
                    jobGroup: '',
                    description: '',
                    jobClassName: '',
                    cronExpression: '',
                    nextTime: '',
                    prevTime: '',
                    triggerName: '',
                    triggerGroup: ''
                },
                idx: -1,
                btnStatus: true
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    console.log(100);
                    return d;
                    // let is_del = false;
                    // for (let i = 0; i < this.del_list.length; i++) {
                    //     if (d.name === this.del_list[i].name) {
                    //         is_del = true;
                    //         break;
                    //     }
                    // }
                    // if (!is_del) {
                    //     if (d.address.indexOf(this.select_cate) > -1 &&
                    //         (d.name.indexOf(this.select_word) > -1 ||
                    //             d.address.indexOf(this.select_word) > -1)
                    //     ) {
                    //         return d;
                    //     }
                    // }
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
                console.log('8888')
                console.log(val);
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.get_url,
                    data: {
                       "page":that.cur_page
                    },
                    success: function (res) {
                        that.total = res.data.total;
                        if (res.data.data == null){
                            that.tableData = [];
                        }else {
                            that.tableData = res.data.data;
                        }
                    }
                });
            },
            search() { /// ...
                this.is_search = true;
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.get_url,
                    data: {
                       "jobName":that.select_word,
                       "page":that.cur_page
                    },
                    success: function (res) {
                        if (res.data.data == null){
                            that.tableData = [];
                        }else {
                            that.tableData = res.data.data;
                        }
                    }
                });
            },
            formatter(row, column) {

            },
            select_State(state){ //选择状态
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.get_url,
                    data: {
                        "triggerState": state,
                        "page":that.cur_page
                    },
                    success: function (res) {
                        if (res.data.data == null){
                            that.tableData = [];
                        }else {
                            that.tableData = res.data.data;
                        }
                    }
                });
            },
            p_r_Formatter(row){
                var that = this;
                //this.btnStatus = this.btnStatus ? false : true ;
                if(row.triggerState == "PAUSED"){
                    return '恢复';
                }else if (row.triggerState == "ACQUIRED"){
                    return '停止';
                }else if (row.triggerState == "WAITING"){
                    return "等待中..";
                }else{
                    return "出错";
                }
            },
            p_r_switch(index, row){
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.pause_resume_url,
                    data: {
                        "triggerName": row.triggerName,
                        "triggerGroup": row.triggerGroup,
                        "triggerState": row.triggerState
                    },
                    success: function (data) {
                        that.$message.success(data.data);
                        that.getData();
                    }
                });
            },
            // 立刻触发job
            trigger(index , row){
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.trigger_url,
                    data: {
                        "jobName": row.jobName,
                         "jobGroup": row.jobGroup,
                    },
                    success: function (data) {
                        that.$message.success(data.data);
                    }
                });
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                if (index == null || row == null){ //新增
                    this.dialog_title = '新增';
                    this.isAdd = true;
                    this.editVisible = true;
                    this.form = {
                        jobName: '',
                        jobGroup: '',
                        description: '',
                        jobClassName: '',
                        cronExpression: '',
                        triggerName: '',
                        triggerGroup: ''
                    }
                    return ;
                }
                //修改
                this.idx = index;
                this.dialog_title = '修改';
                this.isAdd = false;
                const item = this.tableData[index];
                this.form = {
                    jobName: item.jobName,
                    jobGroup: item.jobGroup,
                    description: item.description,
                    jobClassName: item.jobClassName,
                    cronExpression: item.cronExpression,
                    triggerName: item.triggerName,
                    triggerGroup: item.triggerGroup
                }
                this.editVisible = true;
            },
            handleDelete(index, row) { //单个删除
                this.idx = index;
                this.delVisible = true;
            },

            delAll() { //批量删除
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].jobName + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                // this.$set(this.tableData, this.idx, this.form);
                // this.editVisible = false;
                // this.$message.success(`修改第 ${this.idx+1} 行成功`);
                var that = this;
                $.ajax({
                    type: 'post',
                    url: that.save_url,
                    data: {
                        "jobName": that.form.jobName,
                         "jobGroup": that.form.jobGroup,
                        "triggerName":  that.form.triggerName,
                        "triggerGroup": that.form.triggerGroup,
                        "description": that.form.description,
                        "jobClassName": that.form.jobClassName,
                        "cronExpression": that.form.cronExpression,
                        "isAdd": that.isAdd
                    },
                    success: function (data) {
                        that.editVisible = false;
                        that.$message.success('保存成功');
                        that.getData();
                        //this.delVisble = false; //关闭模态框
                    }
                });
            },
            // 确定删除
            deleteRow(){
                var that = this;
                $.ajax({
                    type: 'post',
                    url: this.del_url,
                    data: {
                        "page": this.cur_page,
                        "jobName": this.tableData[this.idx].jobName,
                        "jobGroup": this.tableData[this.idx].jobGroup,
                        "triggerName": this.tableData[this.idx].triggerName,
                        "triggerGroup": this.tableData[this.idx].triggerGroup 
                    },
                    success: function (data) {
                        that.delVisible = false;
                        that.$message.success('删除成功');
                        that.getData();
                        //this.delVisble = false; //关闭模态框
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }
    .handle-select {
        width: 120px;
    }
    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
</style>